#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @author: zzll0909
# @file: 7.py
# @time: 2022/11/16
# @software: VS Code

class Solution:
    def reverse(self, x: int) -> int:
        INT_MIN = -2147483648
        INT_MAX = 2147483647
        result = 0
        while x != 0:
            if result >= INT_MIN // 10 + 1 and result <= INT_MAX // 10:
                digit = x % 10
                # Python3 的取模运算在 x 为负数时也会返回 [0, 9) 以内的结果，因此这里需要进行特殊判断
                if x < 0 and digit > 0:
                    digit -= 10
                # Python3 的整数除法在 x 为负数时会向下（更小的负数）取整，因此不能写成 x //= 10
                x = (x - digit) // 10
                result = result * 10 + digit
            else:
                result = 0
                break
        return result


if __name__ == "__main__":
    x = -1563847412
    solu = Solution()
    result = solu.reverse(x)
    print(result)
    print(-214748365 >= -214748364)
